package com.lin.mapper;

import com.lin.entity.CollectionEntity;
import com.lin.entity.DynamicEntity;
import com.lin.entity.User;
import com.lin.entity.dto.UserDto;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    /**
     * 根据邮箱查询
     * @param email
     * @return
     */
    @Select("select * from user where email = #{email}")
    User findByEmail(String email);
    /**
     * 根据手机号查询
     * @param phone
     * @return
     */
    @Select("select * from user where phone = #{phone}")
    User findByPhone(String phone);

    /**
     * 根据邮箱手机号与账号查询
     * @param userDto
     * @return
     */
    User findByEmailOrPhoneOrUsername(UserDto userDto);

    /**
     * 根据邮箱删除
     * @param email
     * @return
     */
    @Delete("delete from user where email = #{email}")
    Integer deleteUserByEmail(String email);

    /**
     * 添加用户
     * @param user
     */
    void save(User user);

    /**
     * 根据id查询
     * @param userId
     * @return
     */
    @Select("select * from user where user_id = #{userId}")
    User findByUserId(long userId);
    /**
     * 更新用户数据
     * @param user
     */
    void update(User user);

    /**
     * 根据用户id查询收藏夹
     * @param userId
     * @return
     */
    @Select("select * from collection where user_id = #{userId}")
    CollectionEntity getCollection(Integer userId);

    /**
     * 新建收藏夹
     * @param collection
     */
    void saveClt(CollectionEntity collection);

    /**
     * 更新收藏夹
     * @param collection
     */
    void updateClt(CollectionEntity collection);
    //留言查询需要用到的
    @Select("select nickname from user where user_Id=#{userId}")
    String getUsernameById(Integer userId);
    @Select("select avatar from user where user_Id=#{userId}")
    String getAvatarById(Integer userId);

    /**
     * 关注一个用户，提供关注者与被关注者
     * @param fanId
     * @param followedId
     * @return
     */
    @Insert("insert into fans (fanId,followedId) values(#{fanId},#{followedId})")
    int followAUser(Integer fanId, Integer followedId);

    /**
     * 取关一个用户，提供关注者与被关注者
     * @param fanId
     * @param followedId
     * @return
     */
    @Delete("DELETE FROM fans WHERE fanId = #{fanId} and followedId = #{followedId}")
    int cancelFollowAUser(Integer fanId,Integer followedId);

    /**
     * 查找关注用户的人即粉丝
     * @param userId
     * @return
     */
    @Select("select *,#{userId} as askId from user WHERE user.user_id IN (SELECT fanId from fans WHERE followedId= #{userId})")
    List<User> findFans(Integer userId);
    /**
     * 查找用户关注的人即偶像
     * @param userId
     * @return
     */
    @Select("select *,#{userId} as askId from user WHERE user.user_id IN (SELECT followedId from fans WHERE fanId= #{userId})")
    List<User> findFollows(Integer userId);
    /**
     * //根据userId查询动态数量
     * @param userId
     * @return
     */
    @Select("select count(*) from dynamic where user_id = #{userId}")
    Integer getdynamicNumById(Integer userId);
    /**
     * //根据userId查询粉丝数量
     * @param userId
     * @return
     */
    @Select("select count(*) from fans where followedId = #{userId}")
    Integer getFanNumById(Integer userId);

    /**
     * //根据userId查询关注数量
     * @param userId
     * @return
     */
    @Select("select count(*) from fans where fanId = #{userId}")
    Integer getFollowNumById(Integer userId);
}
